<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of change_obj
 *
 * @author Juraj
 */
class change_obj {
    
    private $tb_changes = "";
    
    public function __construct() {
        $this->tb_changes = $GLOBALS['tb_changes'];
    }

    public function add_change($in_data) {
        if (empty($in_data)) {
            return new error_obj('empty $in_data', get_class(), __FUNCTION__);
        }
        
        if (empty($in_data['user_id']))
            $in_data['user_id'] = $_SESSION['user']['user_id'];
        
        $db_obj = new db_obj();
        $result = $db_obj->insert($this->tb_changes, $in_data);
        if (error_obj::isError($result))
            return $result;
        else
            return $db_obj->lastInsertedId();

    }
    
    public function add_change_with_tasks_compare($updatedTaskId, $originalTask) {
        
        $task_obj = new task_obj();
        
        $updatedTask = $task_obj->getTaskById($updatedTaskId);
        if (error_obj::isError($updatedTask))
            return false;
        
        base_obj::debug('updated', $updatedTask);
        base_obj::debug('original', $originalTask);
        
        
        $compared_data = $task_obj->compareTasks($originalTask, $updatedTask);
        
        foreach ($compared_data as $key => $values) {
            $in_data = array(
                'rel_id' => $updatedTaskId,
                'type' => CHANGES_TYPE_TASK,
                'status' => CHANGES_STATUS_UPDATED,
                'attribute' => $key,
                'new' => $values['new'],
                'old' => $values['original'],
                'date' => time()
            );
            $this->add_change($in_data);
        }
    }
    
    public function add_change_with_records_compare($updatedRecordId, $originalRecord) {
        
        $record_obj = new record_obj();
        
        $updatedRecord = $record_obj->getRecordById($updatedRecordId);
        if (error_obj::isError($updatedRecord))
            return false;
        
        base_obj::debug('updated', $updatedRecord);
        base_obj::debug('original', $originalRecord);
        
        
        $compared_data = $record_obj->compareRecords($originalRecord, $updatedRecord);
        
        foreach ($compared_data as $key => $values) {
            $in_data = array(
                'rel_id' => $updatedRecordId,
                'type' => CHANGES_TYPE_TSDR,
                'status' => CHANGES_STATUS_UPDATED,
                'attribute' => $key,
                'new' => $values['new'],
                'old' => $values['original'],
                'date' => time()
            );
            $this->add_change($in_data);
        }
    }
    
    public function get_changes($filter = array(), $order = '', $groupBy = '', $what = '*') {
        $where = '1=1';
        if (!empty($filter)) {
            foreach ($filter as $key => $value) {
                $where .= " AND `$key`='$value'";
            }
        }
        
        if (!empty($order)){
            $order = "ORDER BY $order";
        }
        
        if (!empty($groupBy)){
            $groupBy = "GROUP BY $groupBy";
        }
        
        $sql = "SELECT $what FROM $this->tb_changes WHERE $where $groupBy $order";
        
        $db_obj = new db_obj();
        return $db_obj->getAll($sql);
    }
    
    public function clear_table() {
        $sql = "TRUNCATE TABLE $this->tb_changes";
        
        $db_obj = new db_obj();
        $db_obj->query($sql);
    }
    
}

?>
